﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASoft.Repository;
using System.Collections;
using ASoft.Base;

namespace ASoft.WPF.ViewModel
{
    public partial class WeatherStationInfoViewModel : DBSearchViewModel<WeatherStationInfoEntity>
    {
        protected override IEnumerable<WeatherStationInfoEntity> OrderSearchResult(IEnumerable<WeatherStationInfoEntity> items)
        {
            return items.OrderByDescending(p => p.时间);
        }

        protected override IRepository<WeatherStationInfoEntity> Repository
        {
            get { return new WeatherStationRepository(); }
        }

        protected override IEnumerable<SeriesBindObject> GetSeriesBindObjects(string propertyName)
        {
            var result = new List<SeriesBindObject>();
            result.Add(GetSeriesBindObject(propertyName, this.数据集合));
            return result;
        }

        private SeriesBindObject GetSeriesBindObject(string propertyName, IEnumerable<WeatherStationInfoEntity> iEnumerator)
        {
            var property = typeof(WeatherStationInfoEntity).GetProperty(propertyName);
            var dictType = typeof(Dictionary<,>).MakeGenericType(typeof(DateTime), property.PropertyType);
            var dict = Activator.CreateInstance(dictType) as IEnumerable;
            var addMethod = dictType.GetMethod("Add");
            foreach (var item in iEnumerator)
            {
                addMethod.Invoke(dict, new object[] { item.时间, property.GetValue(item, null) });
            }

            return new SeriesBindObject(propertyName, dict);
        }
    }
}
